<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

class AutogeneratorModelTables extends JModelList
{

    public function __construct($config = array())
    {
        if (empty($config['filter_fields']))
        {
            $config['filter_fields'] = array(
                'id', 'at.id',
                'component_id', 'at.component_id',
                'title', 'at.title',
                'sql_table_name', 'at.sql_table_name',
                'description', 'at.description',
                'status', 'at.status',
                'component', 'ac.title',
                'ordering', 'at.ordering'                
            );
        }

        parent::__construct($config);
    }

    protected function populateState($ordering = null, $direction = null)
    {
        $oApplication = JFactory::getApplication();

        $sLayout = $oApplication->input->get('layout');

        if ($sLayout)
        {
            $this->context .= '.' . $sLayout;
        }

        $sSearch = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
        $this->setState('filter.search', $sSearch);

        $iComponentId = $this->getUserStateFromRequest($this->context . '.filter.component_id', 'filter_component_id');
        $this->setState('filter.component_id', $iComponentId);

        parent::populateState('at.title', 'asc');
    }

    protected function getStoreId($iId = '')
    {
        $iId .= ':' . $this->getState('filter.search');
        $iId .= ':' . $this->getState('filter.component_id');

        return parent::getStoreId($iId);
    }

    public function getItems()
    {
        return parent::getItems();
    }

    protected function getListQuery()
    {
        $oDatabase = $this->getDbo();
        $oQuery = $oDatabase->getQuery(true);

        $oQuery->select($this->getState('list.select', 'at.id, at.component_id, at.title, at.sql_table_name, at.description, at.status, at.ordering, ac.title AS component'))
                ->from('#__auto_table AS at')
                ->join('LEFT', '#__auto_component AS ac ON ac.id = at.component_id')
                ->where('ac.status = 1');

        $iComponentId = $this->getState('filter.component_id');
        if (is_numeric($iComponentId) && $iComponentId > 0)
        {
            $oQuery->where('at.component_id = ' . (int) $iComponentId);
        }

        $sSearch = $this->getState('filter.search');
        if (!empty($sSearch))
        {
            if (stripos($sSearch, 'id:') === 0)
            {
                $oQuery->where('at.id = ' . (int) substr($sSearch, 3));
            }
            else
            {
                $sSearch = $oDatabase->Quote('%' . $oDatabase->escape($sSearch, true) . '%');
                $oQuery->where('(at.title LIKE ' . $sSearch . ' OR at.description LIKE ' . $sSearch . ' OR at.sql_table_name LIKE ' . $sSearch . ')');
            }
        }

        // For ordering.
        $orderCol = $this->state->get('list.ordering', 'at.title');
        $orderDirn = $this->state->get('list.direction', 'asc');
        $oQuery->order($oDatabase->escape($orderCol . ' ' . $orderDirn));
        
        return $oQuery;
    }

    public function deleleAllTablesByComponentId($iComponentId) {
        $oDatabase = JFactory::getDbo();
        
        $oQuery = $oDatabase->getQuery(true);
        $oQuery->delete($oDatabase->quoteName('#__auto_table'));
        $oQuery->where('component_id = ' . (int) $iComponentId);

        $oDatabase->setQuery($oQuery);

        try {
            $bResult = $oDatabase->query();
        } catch (Exception $e) {
            if ($sError = $oDatabase->getErrorMsg()) {
                return $sError;
            }
        }
        
        return $bResult;
    }
    
}
